package org.shop.controller.app;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Merchant;
import org.shop.entity.Orders;
import org.shop.entity.SystemMessage;
import org.shop.entity.Users;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.MerchantService;
import org.shop.service.OrdersService;
import org.shop.service.SystemMessageService;
import org.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SystemMessageController {

	private static Logger logger = LogManager.getLogger(SystemMessageController.class);
	
	@Autowired
	private SystemMessageService systemMessageService;
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private UserService userService;
	
	/**
	 * 获取系统消息
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getSystemMessage")
	public String getSystemMessage(HttpServletRequest req) {
		Map<String,Object> paramMap = new HashMap<String,Object>(); 
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			paramMap.put("receiveId", ud.getUserId());
			List<Map<String,Object>> systemMessageSystemList = systemMessageService.getSystemMessageList(paramMap);
			req.setAttribute("listSystemMessage", systemMessageSystemList);
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "app/systemInfo";
	}
	
	
	
	/**
	 * 获取今天的订单提示
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getToDayOfOrderPrompt")
	@ResponseBody
	public Map<String,Object> getToDayOfOrderPrompt(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> paramMap = new HashMap<String,Object>(); 
		Map<String,Object> paramMapUser = new HashMap<String,Object>(); 
		String orderId = req.getParameter("orderId");
		String orderNo = req.getParameter("orderNo");
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Date d = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String dateNowStr = sdf.format(d); 
        Orders ordersInfo = ordersService.selectByPrimaryKey(Long.parseLong(orderId));
        paramMapUser.put("merchantId", ordersInfo.getMerchantId());
        Users userInfoByConditions = userService.getUserInfoByConditions(paramMapUser);
		try {
			paramMap.put("sendId", ud.getUserId());
			paramMap.put("orderId", orderId);
			Map<String,Object> systemMessageSystem = systemMessageService.getSystemMessageRecently(paramMap);
			SystemMessage systemMessage = new SystemMessage();
			systemMessage.setSendId(ud.getUserId());
			systemMessage.setOrderId(Long.parseLong(orderId));
			systemMessage.setreceiveId(userInfoByConditions.getUserId());
			systemMessage.setMessageType((byte) 2);
			systemMessage.setMessageContent("您的一个订单号为"+orderNo+",需要尽快去发货，希望您能尽快发货完成交易~");
			if(systemMessageSystem!=null&&!"".equals(systemMessageSystem)) {
					System.err.println("createDate------------"+systemMessageSystem.get("createDate")+"today is"+dateNowStr);
					if(systemMessageSystem.get("createDate").equals(dateNowStr)) {
						//今天已经提示过了唔需要在提示
						map.put("num", 0);
					}else {
						int num = systemMessageService.insertSelective(systemMessage );
						map.put("num", num);
					}
			}else {
				int num = systemMessageService.insertSelective(systemMessage );
				map.put("num", num);
			}
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return map;
	}
	
}
